<template>
  <a-drawer v-model:visible="LAYER.visible" :title="LAYER.title" :width="LAYER.width"
            :closable="LAYER.closable" :mask-closable="LAYER.maskClosable" :esc-to-close="LAYER.escToClose"
            :unmount-on-close="LAYER.unmountOnClose" :footer="LAYER.footer" :ok-text="LAYER.okText"
            :ok-loading="LAYER.okLoading" :cancel-text="LAYER.cancelText" :on-before-ok="onSubmit" @close="onCancel">
    <!--表单开始-->

    <a-form ref="formRef" :model="formState" layout="vertical" :rules="MODULE_EDIT.RULES">
      <a-row :gutter="12">
        <a-col :span="12">
          <a-form-item label="模块代码" field="code">
            <a-input v-model="formState.code" allow-clear placeholder="请填写模块代码" :disabled="isUpdate"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="模块名称" field="name">
            <a-input v-model="formState.name" allow-clear placeholder="请填写模块名称"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="模块描述" field="description">
            <a-textarea v-model="formState.description" placeholder="请填写模块描述" :auto-size="{ minRows: 2, maxRows: 5 }"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="模块图标" field="icon">
            <smart-icon-picker v-model="formState.icon" placeholder="请填写模块图标"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="所在包名" field="pkgName">
            <a-input v-model="formState.pkgName" allow-clear placeholder="请填写所在包名"/>
          </a-form-item>
        </a-col>


        <a-col :span="12">
          <a-form-item label="版本号" field="releaseVersion">
            <a-input v-model="formState.releaseVersion" allow-clear placeholder="请填写模块版本号"/>
          </a-form-item>
        </a-col>

        <a-col :span="12">
          <a-form-item label="模块状态" field="state">
            <smart-dict v-model="formState.state" :url="DictOptions.InstallState" placeholder="请选择模块状态"/>
          </a-form-item>
        </a-col>

        <a-col :span="12">
          <a-form-item label="是否存在前端页面" field="hasWeb">
            <smart-bool v-model="formState.hasWeb" true-text="存在" false-text="不存在"/>
          </a-form-item>
        </a-col>


        <a-col :span="12">
          <a-form-item :label="`显示排序值 [ ${formState.sort} ]` " field="sort"
                       tooltip="升序排序 显示值越小 显示越靠前">
            <a-slider v-model="formState.sort"/>
          </a-form-item>
        </a-col>


      </a-row>


    </a-form>
    <!--表单结束-->
  </a-drawer>
</template>

<script lang="ts" setup>

import {EditDrawer} from '@/libs/core'
import {DictOptions} from '@/libs/options'
import {MODULE_EDIT} from "../resource/form";
import {SystemHttp} from '../resource/http'

const props = defineProps({record: {type: Object, required: true}})
const emit = defineEmits(['cancel'])

const {addModule, updateModule} = SystemHttp
const config = {tplRecord: MODULE_EDIT.BODY, rawRecord: props.record, rowKey: 'id', emit}

const service = new EditDrawer(config).setAddRequest(addModule).setUpdateRequest(updateModule)
const {formRef, formState, LAYER, isUpdate, onCancel, onSubmit} = service
LAYER.title = isUpdate ? '修改模块' : '新建模块'
</script>
